<template>
	<view class="content">
		<view class="text-area" ref="input"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		onLoad() {

		},
		mounted() {
			var that = this;
			var input = document.createElement('input')
			input.id = 'uploadInput';
			input.type = 'file'  
			input.onchange = function(e) {  
				var filereader = new FileReader();
				// var fileType = this.files[0].type;
				// console.log(fileType)
				filereader.readAsDataURL(this.files[0]);
				filereader.onload = function(){
					// console.log(this.result);
					that.upload();
				}
			}  
			that.$refs.input.$el.appendChild(input)
		},
		methods: {
			upload(){
				uni.showLoading({
					title: '上传中'
				})
				var xhr = new XMLHttpRequest();
				// xhr.open('post', '/app/uploadFile');
				xhr.open('post', 'http://rx.kswater.com:19092/fHx/imgUpload');
				let uploadInput = document.getElementById('uploadInput');
				let data = new FormData();
				data.append('file', uploadInput.files[0]);
				xhr.send(data);
				xhr.onreadystatechange = function () {
					uni.hideLoading();
					//后端接受完毕
					if(xhr.readyState == 4 && xhr.status==200){
						uni.showToast({
							icon: 'success',
							title: '上传成功'
						})
						 var obj = JSON.parse(xhr.responseText);
						 console.log(obj);
					 }
				};
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
